package com.gwq.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ThreadPoolTaskConfig {

    /**
     * 核心线程数
     */
    private static final int corePoolSize = 5;

    /**
     * 最大线程数
     */
    private static final int maxPoolsize = 5;

    /**
     * 允许线程空闲时间默认时间:s
     */
    private static final int keepAliveTime = 10;

    /**
     * 缓冲队列
     */
    private static final int queueCapaccity = 200;

    /**
     * 线程池前缀
     */
    private static final String threadNamePreix = "Async-Service-";

    @Bean
    public ThreadPoolTaskExecutor taskExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolsize);
        executor.setQueueCapacity(queueCapaccity);
        executor.setKeepAliveSeconds(keepAliveTime);
        executor.setThreadNamePrefix(threadNamePreix);
        //线程池拒绝策略
        //CallerRunsPolicy()由调用线程处理该任务
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //初始化
        executor.initialize();
        return executor;
    }

}

